package cn.xz.dao.impl;

import cn.xz.dao.BaseDaoSingleton;
import cn.xz.dao.IUserDao;
import cn.xz.entity.Happy;
import cn.xz.entity.Userxx;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class IUserDaoImpl implements IUserDao {
    @Override
    public Userxx Longin(String username, String userpassword) {
        Connection con = null;
        PreparedStatement pstat= null;
        ResultSet rs = null;
        Userxx u = null;
        try {
            con = BaseDaoSingleton.getCon();
            String sql="select username,userpassword from user where username= ? and userpassword=?";
            pstat = con.prepareStatement(sql);
            pstat.setString(1,username);
            pstat.setString(2,userpassword);
            rs=pstat.executeQuery();
            System.out.println(sql);
            if(rs.next()){
                u= new Userxx();
                u.setUsername(rs.getString("username"));
                u.setUserpassword(rs.getString("userpassword"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDaoSingleton.closeAll(null,null,null);
        }
        return u;
    }

    @Override
    public List<Userxx> getUserAndDept() {
        Connection con = null;
        Statement statement = null;
        ResultSet rs = null;
        List<Userxx> userxxes = new ArrayList<Userxx>();
        int count = -1;
        try {
            con = BaseDaoSingleton.getCon();
            statement = con.createStatement();
            String sql="select username,name from user LEFT JOIN work on dape=id";
            rs=statement.executeQuery(sql);
            while(rs.next()){
                Userxx u = new Userxx();
                u.setUsername(rs.getString("username"));
                u.getWork().setName(rs.getString("name"));
                userxxes.add(u);


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return userxxes;
    }
}
